<?php
class Welcome extends CI_Controller {
    public function index() {
        // $this->load->view('welcome_message');
        // $this->load->helper('url');
        echo base_url();
        $this->load->view('form.html');
    }
    public function handle() {
        // echo '我将对接收到的数据进行处理！';
        // 载入验证类
        $this->load->library('form_validation');
        // 设置验证规则
        // 验证规则的三个参数分别是字段名、提示信息、验证规则
        $this->form_validation->set_rules('stuname', '姓名', 'callback_username_check');
        $this->form_validation->set_rules('stunumber', '学号', 'callback_stunumber_check');
        // 运行验证规则
        if ($this->form_validation->run()) {
            echo '用户信息通过验证，现在可以进行下一步工作了。';
        } else {
            $this->load->view('form.html');
        }
    }
       /**
        *学生姓名字段检验方法，检验规则通过正则表达式设置
        *@param [type] $str [用户姓名]
        *@return [type]     [返回布尔值，检验通过返回真，否则为假] 
        */
                
       
    public function username_check($str) {
        $pattern = '/^[\x80-\xff]{6,}$/';
        if (!preg_match($pattern, $str)) {
            $this->form_validation->set_message('username_check', '姓名至少为两个汉字，不能有数字和字母');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    public function stunumber_check($str) {
        $pattern = '/^[pP]1[3-7]\d{7}$/';
        if (!preg_match($pattern, $str)) {
            $this->form_validation->set_message('stunumber_check', '学号必须为10位，且开头为p');
            return FALSE;
        } else {
            return TRUE;
        }
    }
    public function re($value = '') {
        $pattern = '/^[\x80-\xff]{6,}$/';
        // $pattern = '/[1-4a-h]/';
        $string = '中';
        $this->outputRe($pattern, $string);
        function test()
        {
          $ch = curl_init("http://www.example.com/");
          $fp = fopen("example_homepage.txt", "w");
          curl_setopt($ch, CURLOPT_FILE, $fp);
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_exec($ch);
          curl_close($ch);
          fclose($fp);
             
        }
         function outputRe($pattern, $string) {
 
        if (preg_match_all($pattern, $string, $matched)) {
            echo '正则表达式:', htmlentities($pattern), '和字符串：', htmlentities($string), '匹配成功，匹配的字符串是：', "<br />";
            echo '<pre>';
            var_dump($matched);
            echo '</pre>';
        } else {
            echo "<p>", '没找到匹配字符', "</p>";
        }
    }
}
 ?>